package com.lzx.dormsys.service.impl;

import java.util.List;

import com.lzx.dormsys.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.lzx.dormsys.bean.DeviceBean;
import com.lzx.dormsys.mapper.DoorBeanMapper;
import com.lzx.dormsys.bean.DoorBean;
import com.lzx.dormsys.service.IDoorBeanService;

/**
 * doorService业务层处理
 * 
 * @author lzx
 * @date 2021-09-10
 */
@Service
public class DoorBeanServiceImpl implements IDoorBeanService 
{
    @Autowired
    private DoorBeanMapper doorBeanMapper;

    /**
     * 查询door
     * 
     * @param doorId doorID
     * @return door
     */
    @Override
    public DoorBean selectDoorBeanById(Long doorId)
    {
        return doorBeanMapper.selectDoorBeanById(doorId);
    }

    /**
     * 查询door列表
     * 
     * @param doorBean door
     * @return door
     */
    @Override
    public List<DoorBean> selectDoorBeanList(DoorBean doorBean)
    {
        return doorBeanMapper.selectDoorBeanList(doorBean);
    }

    /**
     * 新增door
     * 
     * @param doorBean door
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDoorBean(DoorBean doorBean)
    {
        int rows = doorBeanMapper.insertDoorBean(doorBean);
        insertDmsDevice(doorBean);
        return rows;
    }

    /**
     * 修改door
     * 
     * @param doorBean door
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDoorBean(DoorBean doorBean)
    {
        doorBeanMapper.deleteDeviceBeanByDeviceId(doorBean.getDoorId());
        insertDmsDevice(doorBean);
        return doorBeanMapper.updateDoorBean(doorBean);
    }

    /**
     * 批量删除door
     * 
     * @param doorIds 需要删除的doorID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDoorBeanByIds(Long[] doorIds)
    {
        doorBeanMapper.deleteDeviceBeanByDeviceIds(doorIds);
        return doorBeanMapper.deleteDoorBeanByIds(doorIds);
    }

    /**
     * 删除door信息
     * 
     * @param doorId doorID
     * @return 结果
     */
    @Override
    public int deleteDoorBeanById(Long doorId)
    {
        doorBeanMapper.deleteDeviceBeanByDeviceId(doorId);
        return doorBeanMapper.deleteDoorBeanById(doorId);
    }

    /**
     * 新增dms信息
     * 
     * @param doorBean door对象
     */
    public void insertDmsDevice(DoorBean doorBean)
    {
        List<DeviceBean> deviceList = doorBean.getDmsDeviceList();
        Long doorId = doorBean.getDoorId();
        if (StringUtils.isNotNull(deviceList))
        {
            List<DeviceBean> list = new ArrayList<DeviceBean>();
            for (DeviceBean deviceBean : deviceList)
            {
                deviceBean.setDeviceId(doorId);
                list.add(deviceBean);
            }
            if (list.size() > 0)
            {
                doorBeanMapper.batchDeviceBean(list);
            }
        }
    }
}
